﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using demo.Common;
using iAgileFramework.DataTransCarrier;

namespace WebApplication1.Controllers
{
    public class WebUploaderController : Controller
    {
        #region 插件约束
        //定义允许上传的文件扩展名
        private static readonly Dictionary<string, string> _ExtDictionary = new Dictionary<string, string>()
        {
            {"image", "gif,jpg,jpeg,png,bmp"},
            {"flash", "swf,flv"},
            {"media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb"},
            {"file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2,pdf"}
        };

        //最大图片大小
        private const int _MaxImageSize = 1048576; //1MB 
        #endregion
        [HttpPost]
        public ActionResult UploadImg()
        {
            #region 有效性验证
            //没有文件返回
            var rv = new ReturnValues<string>();
            if (base.Request.Files.Count == 0)
            {
                rv.Fail("无效的文件");
                return this.Json(rv);
            }

            HttpPostedFileBase fileControl = base.Request.Files[0];
            if ((fileControl == null) || (fileControl.ContentLength <= 0))
            {
                rv.Fail("无效的文件");
                return this.Json(rv);
            }

            if (fileControl.ContentLength > _MaxImageSize)
            {
                rv.Fail("上传图片太大，最多1MB");
                return this.Json(rv);
            }

            var fileName = fileControl.FileName;
            if (fileName == null)
            {
                rv.Fail("无效的文件");
                return this.Json(rv);
            }

            var fileExt = Path.GetExtension(fileName).ToLower();
            if (Array.IndexOf((_ExtDictionary["image"]).Split(','), fileExt.Substring(1).ToLower()) == -1)
            {
                rv.Fail(string.Format("格式必须是{0}中的一种", _ExtDictionary["image"]));
                return this.Json(rv);
            }

            #endregion

            #region 保存文件
            var accessPath = Request.ApplicationPath + "temp/";//访问路径
            var savePath = base.Server.MapPath(accessPath);        //保存路径
            var thumbFileName = "";//缩略图文件名
            if (!Directory.Exists(savePath)) Directory.CreateDirectory(savePath);

            //生成文件名
            var random = new Random();
            fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + random.Next(0x3e8, 0x270f) + fileExt;

            try
            {
                fileControl.SaveAs(Path.Combine(savePath, fileName));
                //生成缩略图
                thumbFileName = "thumb_" + fileName;
                Thumbnail.MakeThumbnailImage(savePath + fileName, savePath + thumbFileName,
                    150, 150, "Cut");

            }
            catch (Exception ex)
            {
                rv.Fail(ex.Message);
                return this.Json(rv);
            }
            #endregion
            //有生成缩略图就返回大图路径+“|”+缩略图路径
            rv.Success(thumbFileName == "" ? accessPath + fileName : accessPath + fileName + "|" + accessPath + thumbFileName);
            return this.Json(rv, "text/html");
        }

        public ActionResult Index()
        {
            return View();
        }
    }
}